package sg.edu.nus.iss.ooad.jass.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.entity.JobAdvertisement;
import sg.edu.nus.iss.ooad.jass.ui.widget.JASSRegularPanel;

/**
 *
 * List of Job Advertisement form.
 *
 * @author Wang Juan
 * @version 1.0
 * @since 1.0
 *
 */
public class AdvertiserListJobAdvertisementForm extends JASSRegularPanel {

        private static final long serialVersionUID = -8039887094924252305L;

        private static final Logger logger = Logger.getLogger(AdvertiserListJobAdvertisementForm.class.getName());

        //This variable holding a collections of job advertisement
        private java.util.List<JobAdvertisement> jobs = null;

        // Form Components
        private Button btnSearch = null;
        private Choice choiceSearchBy = null;
        private List searchList = null;
        private TextField textKeyWord = null;
        private Label labelInformation = null;
        private Label labelHeader = null;
        private Panel headerPanel = null;
        private Panel searchCriteriaPanel = null;
        private Panel searchResultPanel = null;
        private Panel notePanel = null;
        private Label noteLabel = null;


        /**
         * Constructor of the AdvertiserListJobAdvertisementForm class
         * @param title
         */
        public AdvertiserListJobAdvertisementForm(String title) {
                super(title);
                initComponents();
                setVisible(true);
        }

        /**
         * Initialize form components
         */
        private void initComponents() {
                jobs = new ArrayList<JobAdvertisement>();
                
                //Fields
                textKeyWord = new TextField(30);
                choiceSearchBy = new Choice();
                btnSearch = new Button("Search");
                labelInformation = new Label("Search Result");
                labelHeader= new Label("No, Ads. Date, Job Title, Technical Skills, Management Skills, Job Location, Min. Salary, Max. Salary");

                searchList = new List(20, false);
                searchList.setSize(new Dimension(400, 300));
                textKeyWord.setText("Job Key Words");
                choiceSearchBy.add("By Title");
                choiceSearchBy.add("By Skills");
                choiceSearchBy.add("By Company");
                choiceSearchBy.add("By Job Location");
                choiceSearchBy.add("By Salary (Greater or Equal)");
                choiceSearchBy.add("By Advertisement Date (n Days before)");

                //Panel
                searchCriteriaPanel = new Panel();
                searchCriteriaPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
                searchCriteriaPanel.add(textKeyWord);
                searchCriteriaPanel.add(choiceSearchBy);
                searchCriteriaPanel.add(btnSearch);

                headerPanel = new Panel();
                headerPanel.setLayout(new GridLayout(2,1, 5,5));
                headerPanel.add(labelInformation);
                headerPanel.add(labelHeader);
                searchResultPanel = new Panel();

                notePanel =new Panel ();
                notePanel.setLayout(new BorderLayout(5,5));
                noteLabel = new Label ("Select item to view the details", Label.CENTER);
                noteLabel.setForeground(Color.RED); 
                notePanel.add( noteLabel); 
                
                searchResultPanel.setLayout(new BorderLayout(5,5));
                searchResultPanel.add(headerPanel, BorderLayout.NORTH);
                searchResultPanel.add(searchList, BorderLayout.CENTER);
                searchResultPanel.add(notePanel, BorderLayout.SOUTH);
                //attached component
                attachComponent(searchCriteriaPanel, 0, 1, 10, 1);
                attachComponent(searchResultPanel, 0, 2, 20, 20);
        }

        /**
         *  Add search action listener to search button.
         * @param listener ActionListener
         */
        public void addSearchActionListener(ActionListener listener) {
                btnSearch.addActionListener(listener);
        }

        /**
         * Add item action listener to search list item.
         * @param listener ItemListener
         */
        public void addJobListItemListener(ItemListener listener) {
                searchList.addItemListener(listener);
        }

        /**
         * Set search  criteria.
         * @param index
         */
        public void setSearchCriteria(int index) {
                choiceSearchBy.select(index);
        }

        /**
         * Get selected item from search criteria
         * @return
         */
        public String getSearchCriteria() {
                return choiceSearchBy.getSelectedItem();
        }

        /**
         * Set the search key words
         * @param text
         */
        public void setSearchKeyword(String text) {
                textKeyWord.setText(text);
        }

        /**
         * Get the search key words
         * @return
         */
        public String getSearchKeyword() {
                return textKeyWord.getText();
        }

        /**
         * Set the text to labelInformation.
         * @param text
         */
        public void setSearchInfo (String text) {
                labelInformation.setText("Search Result: " + text);
        }

        /**
         * Update Job advertisement list to search list . 
         * @param jobs
         */
        public synchronized void updateJobList(java.util.List<JobAdvertisement> jobs) {
                this.jobs.clear();
                this.jobs = jobs;
                searchList.removeAll();
                for (int i = 0; i< jobs.size(); i++) {
                        JobAdvertisement job = jobs.get(i);
                        searchList.add( String.valueOf(i+1) + ". " + job.getStartDate() + ", " + job.getJobTitle() + ", " +  job.getTechnicalSkills() + ", " + job.getManagementSkills() + ", " +  job.getJobLocation() + ", " + String.valueOf(job.getMinSalary()) + " - " + String.valueOf(job.getMaxSalary()));
                }
        }

        /**
         * Get the selected job.
         * @return
         */
        public JobAdvertisement getSelectedJob() {
                JobAdvertisement job = null;

                if (jobs.size() > 0) {
                        job  = jobs.get(searchList.getSelectedIndex());
                }

                return job;
        }



}
